﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAlgorithm
{
    class BruteForceAttack
    {
        public BruteForceAttack()
        {
            int testCase = Int32.Parse(TestConsole.ReadLine());
            while (testCase-- > 0)
            {
                string[] input = TestConsole.ReadLine().Split(' ');
                int a = Int32.Parse(input[0]);
                int b = Int32.Parse(input[1]);
                int n = Int32.Parse(input[2]);
                long p = 1;
                long result = 0;
                long ap = 1;
                for (int i = 1; i <= b - a + 1; i++)
                {
                    p *= n;
                    result += p;
                    if (i == a)
                    {
                        ap = p;
                    }
                }
                for (int i = b - a + 2; i <= a; i++)
                {
                    ap *= n;
                }
                result *= ap;
                Console.WriteLine(result % 1000000007);
            }
           
        }
    }
}
